<template>
    <el-container class="home-container">
<!--        头部-->
        <el-header>
            <div>
                <img src="../assets/heima.png" alt="">
                <span>电商后台管理系统</span>
            </div>
            <el-button type="info" @click="logout">退出登录</el-button>
        </el-header>
        <el-container>
<!--            侧边栏-->
            <el-aside width="isCollapse?'64px':'200px'">
                <div class="toggle-button" @click="show">|||</div>
<!--                侧边栏菜单区域-->
                <el-menu
                        default-active="2"
                        class="el-menu-vertical-demo"
                        background-color="#545c64"
                        text-color="#fff"
                        active-text-color="#ffd04b"
                        :unique-opened="true"
                        :collapse="isCollapse"
                        :collapse-transition="false"
                        :router="true"
                        :default-active="activePath">
<!--                    一级菜单-->
<!--                    下面这句话的意思是：
                            循环遍历menuList生成这些列表，
                            列表名为item中的authName
                            :key:的意思是保持当前列表已经选中打开的状态.-->
                    <el-submenu :index="item.id+''" v-for="item in menuList" :key="item.id">
                        <template slot="title">
<!--                            图标-->
                            <i :class="iconsObj[item.id]"></i>
<!--                            文本区域-->
                            <span>{{item.authName}}</span>
                        </template>
<!--                        二级菜单-->
                        <el-menu-item :index="'/'+SubItem.path"
                                      v-for="SubItem in item.children"
                                      :key="SubItem.id"
                                      @click="saveNavState('/'+SubItem.path)">
<!--                            设置二级菜单模板-->
                            <i class="el-icon-right"></i>
                            <!--                            文本区域-->
                            <span>{{ SubItem.authName }}</span>
                        </el-menu-item>
                    </el-submenu>
                </el-menu>
            </el-aside>
<!--            主体-->
            <el-main>
<!--                路由占位符-->
                <router-view></router-view>
            </el-main>
        </el-container>
    </el-container>
</template>

<script>
    export default {
        data() {
            return {
                menuList: [],
                iconsObj: {
                    125:'el-icon-user-solid',
                    103:'el-icon-setting',
                    101:'el-icon-s-goods',
                    102:'el-icon-s-order',
                    145:'el-icon-s-marketing'
                },
                isCollapse: false,
                //被激活的链接地址
                activePath: ''
            }
        },
        //定义一个生命周期函数
        created() {
            //获取所有菜单
            this.getMenuList();
            this.activePath = window.sessionStorage.getItem('avtivePath');
        },
        name: "Home",
        methods: {
            logout() {
                //清空token
                window.sessionStorage.clear();
                //再次跳转到login页面
                this.$router.push('/login');
            },
            async getMenuList() {
                const {data: res} = await this.$http.get('menus');
                if (res.meta.status !== 200) return this.$message.error(res.meta.msg);
                this.menuList = res.data;
                console.log(res);
            },
            //点击按钮,切换菜单的折叠
            show() {
                this.isCollapse = !this.isCollapse;
            },
            //保存链接的激活状态
            saveNavState(avtivePath) {
                window.sessionStorage.setItem('activePath',avtivePath);
                this.activePath = avtivePath;
            }
        }
    }
</script>

<style lang="less" scoped>
    .home-container {
        height: 100%;
    }
    .el-header {
        background-color: #373D41;
        //把页面设置为弹性布局。
        display: flex;
        //
        justify-content: space-between;
        padding-left: 0;
        align-items: center;
        color: cadetblue;
        font-size: 20px;
        > div {
            display: flex;
            align-items: center;
            span {
                margin-left: 15px;
            }
        }
    }
    .el-aside {
        background-color: #333744;
        .el-menu {
            border-right: none;
        }
    }
    .toggle-button {
        background-color: #4A5064;
        font-size: 10px;
        line-height: 24px;
        color: #ffffff;
        text-align: center;
        letter-spacing: 0.2em;
        cursor: pointer;
    }
</style>